﻿$(document).ready(function() {
    saveDataAjaxSubmit();
});
var initMessages = {};
function saveDataAjaxSubmit() {
    var options = {
        type: 'POST',
        beforeSubmit: showRequest,  // pre-submit callback         
        success: showResponse,  // post-submit callback 
        error: function () {
            alert('Error');
        },
        dataType: 'json',
    };
    try {
        jQuery("#saveData").validate({
            messages: initMessages,
            submitHandler: function (form) {
                if ($('form').serialize() != $('form').data('initialData')) {
                    jQuery(form).ajaxSubmit(options);
                }
            }
        });        
    } catch (e) { }

}
function loadShare() {
    var m1 = 37;
    var m2 = 0;
    var p = document.getElementById('ReportToolbar_Menu');
    if (p != null) {
        function m() {
            var top = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
            if (top + m2 < m1) {
                p.style.top = m1 - top + 'px';
                $(p).css({ 'box-shadow': '0px 0px 0px #A59E94' });
                //$(p).css({ 'position': 'relative' });
            }
            else {
                p.style.top = m2 + 'px';
                $(p).css({ 'box-shadow': '0px 5px 5px #A59E94' });
                $(p).css({ 'position': 'fixed' });
            }
        }
        m();
        window.onscroll = m;
    }
}
function showRequest(formData, jqForm, options) {
    return true;
}
function showResponse(responseText, statusText, xhr, $form) {
    if (responseText == null) {
        responseText = { 'message': null };
    }
    if(responseText.message==null) {
        alert('Update success');
        window.parent.$.colorbox.close();
        window.parent.listViewEventListener();
    } else {
        alert(responseText.message);
    }
}
$(function () {
    $('form').each(function () {
        $(this).data('initialData', $(this).serialize());
    }).submit(function (e) {
        e.preventDefault();
        //if ($(this).serialize() != $(this).data('initialData')) {
        //    alert('Form data has changed');
        //} else {
        //    alert('Form data is the same');
        //}
    });
});
